import { setStore, getStore } from '@/util/store'
import { Base64 } from 'js-base64';
import console from '@/util/console'

const zeus = {
  state: {
    openId: 'init',
    orgTree: '',
    genOrgTree: '',
    projectOrgTree: getStore({ name: (getStore({name: 'userInfo'})? getStore({name: 'userInfo'}).userId : '')+'projectOrgTree'}) || {},
    tenantId: '',
    recordChanged:{},
    projectList:[],
    timeOutFlag: false,
    msgObject: {},
    socketConnection: null,
    showToastMsg: false
  },
  actions: { 
    setOpenId: ({ commit }, v) => commit('SET_OPENID', v),
    setOrgTree: ({ commit }, v) => commit('SET_ORG_TREE', v),
    setGenOrgTree: ({ commit }, v) => commit('SET_GEN_ORG_TREE', v),
    setProjectOrgTree: ({ commit }, v) => commit('SET_PROJECT_ORG_TREE', v),
    setTenantId: ({ commit }, v) => commit('SET_TENANT_ID', v),
    setRecordChanged: ({ commit }, v) => commit('SET_RECORD_CHANGED', v),
    setProjectList: ({ commit }, v) => commit('SET_PROJECT_LIST', v),
    setTimeOutFlag: ({ commit }, v) => commit('SET_TIME_OUT_FLAG', v),
    setMsgObject: ({ commit }, v) => commit('SET_MSG_OBJECT', v),
    setSocketConnection: ({ commit }, v) => commit('SET_SOCKET_CONNECTION', v),
    setShowToastMsg: ({ commit }, v) => commit('SET_SHOW_TOAST_MSG', v)
  },
  mutations: {
    SET_OPENID: (state, v) => {
      console.log('v',v)
      state.openId = v;
      //setStore({ name: 'token', content: state.token, type: 'session' })
    },
    SET_ORG_TREE: (state, v) => {
      state.orgTree = v;
    },
    SET_GEN_ORG_TREE: (state, v) => {
      state.genOrgTree = v;
    },
    SET_PROJECT_ORG_TREE: (state, v) => {
      state.projectOrgTree = v;
      setStore({ name: (getStore({name: 'userInfo'})? getStore({name: 'userInfo'}).userId : '')+'projectOrgTree', content: state.projectOrgTree, type: 'session' })

    },
    SET_TENANT_ID: (state, v) => {
      state.tenantId = v;
    },
    SET_RECORD_CHANGED: (state, v) => {
      state.recordChanged = v;
    },
    SET_PROJECT_LIST: (state, v) => {
      state.projectList = v;
    },
    SET_TIME_OUT_FLAG: (state, v) => {
      state.timeOutFlag = v;
    },
    SET_MSG_OBJECT: (state, v) => {
      state.msgObject = v;
    },
    SET_SHOW_TOAST_MSG: (state, v) => {
      state.showToastMsg = v;
    },
    SET_SOCKET_CONNECTION: (state, v) => {
      const that = this;
      let socket = null;
      if (typeof (WebSocket) == 'undefined') {
        this.$notify({
          title: '提示',
          message: '当前浏览器无法接收实时报警信息，请使用谷歌浏览器！',
          type: 'warning',
          duration: 0
        });
      } else {
        // 实例化socket，这里我把用户名传给了后台，使后台能判断要把消息发给哪个用户，其实也可以后台直接获取用户IP来判断并推送
        const socketUrl = 'ws://socket.dooleen.com:8012/webSocket/sendMsg/'+v.tenantId+'/PC/userControlCommand/'+v.userName+'/'+v.realName+'/'+ Base64.encode(v.avatar || 'abc');
        if(socket == null){
          socket = new WebSocket(socketUrl);
          state.socketConnection = socket;
        }
        
        // 监听socket打开
        socket.onopen = function() {
          console.log('=>>消息内容 浏览器WebSocket已打开');
          state.socketConnection = socket;
        };
        // // 监听socket消息接收
        // this.socket.onmessage = function(msg) {
        //   that.$console.log('消息内容002 msg==',msg);
        //   // let msgData = JSON.parse(msg.data);
        //   // that.userData = msgData.userList;
        //   // that.userList = msgData.userList;
        // };
        // 监听socket错误
        socket.onerror = function() {
          console.error('==>>消息内容 接收通知，服务器错误，无法接收实时信息');
        };
        // 监听socket关闭
        socket.onclose = function() {
          socket = null;
          console.log('消息内容 WebSocket已关闭');
        }
      }
    },
  }

}
export default zeus